cc.Class({
    extends: cc.Component,

    properties: {
        pF: cc.Sprite,
        pDraw: cc.Sprite,
        pBoard: cc.Sprite,
        picCamera: cc.Camera,
        curColorSor: cc.Node,
        alphaSlider : cc.Slider,
        alphaLb : cc.Label,
        curColorNode : cc.Node,
    },
    onLoad() {
        this.pDraw.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.pDraw.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchStart, this);

        cc.dynamicAtlasManager.enabled = false;
        cc.dynamicAtlasManager.maxFrameSize = 8;

        this.curAlpha = 1.0;
    },
    onTouchStart(evt) {
        var location = evt.getLocation();
        var np = this.pDraw.node.convertToNodeSpaceAR(location);

        var ts = new cc.Vec2();
        ts.x = this.pDraw.node.width;
        ts.y = this.pDraw.node.height;

        var cp = new cc.Vec2(); //对比图片百分比。
        cp.x = (np.x + ts.x / 2) / ts.x;
        cp.y = 1 - (np.y + ts.y / 2) / ts.y;
        this.drawMaterial.setProperty("cp", cp);
        this.drawMaterial.setProperty("ts", ts);

        //截图截图一次。
        this.picCamera.render(this.pDraw.node);
    },
    onSliderAlpha( evt ) {
        this.curAlpha = this.alphaSlider.progress;
        this.alphaLb.string = (this.curAlpha*100).toFixed(0);
        this.refreshBlush();
    },
    start() {
        var w = this.pF.node.width;
        var h = this.pF.node.height;
        this.pDraw.node.width = w;
        this.pDraw.node.height = w;
        this.pBoard.node.width = w;
        this.pBoard.node.height = w;
        this.drawMaterial = this.pDraw.getMaterial(0);
        this.rt = new cc.RenderTexture();
        this.rt.initWithSize(this.pF.node.width, this.pF.node.height);
        this.rt.setFilters(cc.Texture2D.Filter.LINEAR, cc.Texture2D.Filter.LINEAR );
        this.rt.setPremultiplyAlpha( false );
        this.picCamera.targetTexture = this.rt;
        this.picCamera.enabled = false;
        this.spriteFrame = new cc.SpriteFrame(this.rt);
        this.pBoard.spriteFrame = this.spriteFrame;

        this.drawMaterial.setProperty("textureLine", this.pF.spriteFrame.getTexture());
    },

    onClickColor(evt, val) {
        var node = evt.currentTarget;
        this.curColorNode = node;

        this.refreshBlush();

        this.curColorSor.setPosition(
            this.node.convertToNodeSpaceAR(node.convertToWorldSpaceAR(cc.Vec2.ZERO)));
    },
    onClickClean(){
        this.rt.initWithSize(this.pF.node.width, this.pF.node.height);
        this.rt.setFilters(cc.Texture2D.Filter.LINEAR, cc.Texture2D.Filter.LINEAR );
        this.rt.setPremultiplyAlpha( false );
        this.picCamera.targetTexture = this.rt;
        this.pBoard.spriteFrame.setTexture( this.rt );
    },
    refreshBlush(){
        var node = this.curColorNode;
        this.drawMaterial.setProperty("curColorR", node.color.r / 255);
        this.drawMaterial.setProperty("curColorG", node.color.g / 255);
        this.drawMaterial.setProperty("curColorB", node.color.b / 255);
        this.drawMaterial.setProperty("curColorA", this.curAlpha );

        cc.log("rgba:", node.color.r / 255,
            node.color.g / 255,
            node.color.b / 255,
            this.curAlpha );
    },
});
